<template>
    <el-button type="primary" size="large" @click="getnew">新建</el-button>

    <el-table :data="tableData">
        <el-table-column prop="empno" label="员工编号" width="180" />
        <el-table-column prop="ename" label="姓名" width="180" />
        <el-table-column prop="job" label="岗位" width="180" />
        <el-table-column prop="mgr" label="领导编号" width="180" />
        <el-table-column prop="hiredate" label="入职时间" width="180" />
        <el-table-column prop="sal" label="工资" width="180" />
        <el-table-column prop="comm" label="奖金" width="180" />
        <el-table-column prop="deptno" label="部门编号" />

        <el-table-column prop="deptno" label="部门">
            <template #default="scope">
                {{
                    scope.row.dept == null ? '' : scope.row.dept.dname
                }}
            </template>
        </el-table-column>
    </el-table>


    <el-dialog v-model="dialogVisible" title="注册员工" width="30.9%">
        <el-form  label-width="80px">
            <el-form-item label="员工编号">
                <el-input v-model="form.empno" placeholder="请输入员工编号" />
            </el-form-item>
            <el-form-item label="姓名">
                <el-input v-model="form.ename" placeholder="请输入姓名" />
            </el-form-item>
            <el-form-item label="岗位">
                <el-input v-model="form.job" placeholder="请输入岗位" />
            </el-form-item>
            <el-form-item label="领导编号">
                <el-input v-model="form.mgr" placeholder="请输入领导编号" />
            </el-form-item>
            <el-form-item label="入职时间">
                <el-input v-model="form.hiredate" placeholder="请输入入职时间" />
            </el-form-item>
            <el-form-item label="工资">
                <el-input v-model="form.sal" placeholder="请输入工资" />
            </el-form-item>
            <el-form-item label="奖金">
                <el-input v-model="form.comm" placeholder="请输入奖金" />
            </el-form-item>
            <el-form-item label="部门">
                <el-select  @click="loaddate" v-model="value" class="m-2" placeholder="请选择员工所在部门" @change="valueChange"
                    @key-value="value">
                    <el-option v-for="item in options" :key="item.deptno" :label="item.dname" :value="item.deptno" />
                </el-select>
            </el-form-item>
            <el-form-item>
                <el-button @click="add" type="primary">新增</el-button>
                <el-button @click="notadd" type="primary">取消</el-button>
            </el-form-item>
            <!-- <el-form-item label="部门编号">
            <el-input v-model="form.deptno" placeholder="请输入部门编号" />
        </el-form-item> -->
        </el-form>
    </el-dialog>



</template>




<script setup>
import { ref } from 'vue';
import $ajax from '../../utils/axios'



const dialogVisible = ref(false);
const tableData = ref([])
const form = ref({
    empno: '1234',
    ename: 'zzj',
    job: 'CLERK',
    mgr: '7782',
    hiredate: '1982-01-23',
    sal: '1200.00',
    comm: '1',
    deptno: '20'
})//页面展示数据



const options = ref([
    {
        deptno: '',
        dname: '请选择'
    }
])//新增时储存部门数
const value=ref("")//新增时所选部门

// 为表格加载数据
const loaddata = async () => {
    // 1.定义请求路径
    const path = "/emp/q/all"
    // 发同步请求
    // 发完ajax请求以后，进入等待状态，直到服务器作出相应或者超时，
    // 并且返回服务器响应的数据，存到res中
    // const res= await $ajax.get(path)//只有成功了才会执行下一行
    // 2.解构data属性
    const { data: res } = await $ajax.get(path)
    //3.应用结果
    console.log(res);
    // tableData.value=res.data.result;
    if (res.code === 200) {
        tableData.value = res.result;
    }
}
loaddata()

//主页面新增函数
const getnew = () => {
    dialogVisible.value = true;
}

// 员工注册 PUT
const add = () => {

    form.value.deptno=value.value;
    dialogVisible.value = false;
    const path="dept/add";
    $ajax.put(path, form.value, {
      headers: {
        'Content-Type': 'application/json;charset=utf-8'
      }
    })
    //.then(res => {
    //   form.value.deptno = res.data.result;
    //   let obj = JSON.parse(JSON.stringify(form.value));

    //   tableData.value.push(obj);
    //   console.log("PUT 请求成功", res.data);
    //   dialogVisible.value = false;
   // })
}



const notadd= () => {
    dialogVisible.value = false;
}

let loaddate = async () => {
    const path = '/dept/q/all'
    const { data: res } = await $ajax.get(path)
    if (res.code === 200) {
        
        options.value = res.result
        console.log(options.value);
    }
}
</script>

  <!-- 加一个dialog对话框，填上form内容，用select组件完成部门编号的录入，
添加新员工 -->